const axios = require('axios')
const { Message } = require('element-ui');
const router = require('../../router/index');

module.exports={
    namespaced: true,
    state:{
        getForRentCarsData:[],
        getToAuditCarsData:[],
        getSoldOutCarsData:[],
        getReFuseCarsData:[],
        dialogVisible:false,
        updateRentCars:{
            id:'',
            status:''
        },
    pageData: {
        pageSize: 3,
        currentPage: 1,
        pages: 0,
        total: 0,
        rows: []
    },
    pageData2: {
        pageSize: 3,
        currentPage: 1,
        pages: 0,
        total: 0,
        rows: []
    },
    pageData3: {
        pageSize: 3,
        currentPage: 1,
        pages: 0,
        total: 0,
        rows: []
    },
    pageData4: {
        pageSize: 3,
        currentPage: 1,
        pages: 0,
        total: 0,
        rows: []
    },
    },
    mutations:{
        getForRentCars(state,data){
            state.getForRentCarsData=data
            // console.info(state.getForRentCarsData)
        } ,
        getToAuditCars(state,data){
            state.getToAuditCarsData=data
        },
        getSoldOutCars(state,data){
            state.getSoldOutCarsData=data
        },
        getReFuseCars(state,data){
            state.getReFuseCarsData=data
        },
        updateForRentCars(state,row){
            state.updateRentCars={...row}
            console.info(state.updateRentCars)

            state.dialogVisible=true
        },
        close(state){
            state.dialogVisible=false
        },
        changeStatus(state,newStates){
            switch(newStates){
                case '通过': state.updateRentCars.carStatus="待出租";
                break;
                case '不通过':state.updateRentCars.carStatus="已下架"
            }
            

            console.info(state.updateRentCars)
        }
        
    },
    actions:{

        //====================================分页功能==========================================
        handleSizeChange1(context, val) {
            context.state.pageData.pageSize = val
            context.dispatch('getForRentCarsAsync')
            // context.dispatch('getOrdersAsync')
            
        },
        handleCurrentChange1(context, val) {
            context.state.pageData.currentPage = val
            context.dispatch('getForRentCarsAsync')
            // context.dispatch('getOrdersAsync')   
        },

        handleSizeChange2(context, val) {
            context.state.pageData2.pageSize = val
            context.dispatch('getToAuditCarsAsync')
            context.dispatch('getReFuseCarsAsync')
            
        },
        handleCurrentChange2(context, val) {
            context.state.pageData2.currentPage = val
            context.dispatch('getToAuditCarsAsync')
            context.dispatch('getReFuseCarsAsync')   
        },
        handleSizeChange3(context, val) {
            context.state.pageData3.pageSize = val
            context.dispatch('getSoldOutCarsAsync')
            // context.dispatch('getOrdersAsync')
            
        },
        handleCurrentChange3(context, val) {
            context.state.pageData3.currentPage = val
            context.dispatch('getSoldOutCarsAsync')
            // context.dispatch('getOrdersAsync')   
        },

        handleSizeChange4(context, val) {
            context.state.pageData4.pageSize = val
            context.dispatch('getReFuseCarsAsync')
            context.dispatch('getToAuditCarsAsync')

            
        },
        handleCurrentChange4(context, val) {
            context.state.pageData4.currentPage = val
            context.dispatch('getReFuseCarsAsync')
            context.dispatch('getToAuditCarsAsync')
  
        },

        //======================================分页功能===========================================

        //获取待出租车辆
        async getForRentCarsAsync(context){
           const {data}=await axios.get('/cars/getCars',{
               params:{
                carStatus:'待出租',
                pageSize:context.state.pageData.pageSize,
                currentPage:context.state.pageData.currentPage
               }
           });

           console.info(data.rows)
           context.state.pageData.total=data.total
           context.commit('getForRentCars',data.rows)
        },
        
        //获取待审核车辆
        async getToAuditCarsAsync(context){
            const {data}=await axios.get('/cars/getCars',{
                params:{
                 carStatus:'待审核',
                 pageSize:context.state.pageData2.pageSize,
                 currentPage:context.state.pageData2.currentPage
                }
            });
            context.state.pageData2.total=data.total
            context.commit('getToAuditCars',data.rows)
         },

         //获取已出租车辆
         async getSoldOutCarsAsync(context){
            const {data}=await axios.get('/cars/getCars',{
                params:{
                 carStatus:'已出租',
                 pageSize:context.state.pageData3.pageSize,
                 currentPage:context.state.pageData3.currentPage
                }
            });
            context.state.pageData3.total=data.total
            context.commit('getSoldOutCars',data.rows)
         },

         //获取未通过审核车辆
         async getReFuseCarsAsync(context){
            const {data}=await axios.get('/cars/getCars',{
                params:{
                 carStatus:'已下架',
                 pageSize:context.state.pageData4.pageSize,
                 currentPage:context.state.pageData4.currentPage
                }
            });
            context.state.pageData4.total=data.total
            context.commit('getReFuseCars',data.rows)
         },

         //获取车辆详情，并修改车辆状态
         async updateForRentCarsAsync(context,_id){
             console.info(_id)
            const {data}=await axios.post('/cars/uptateCars',context.state.updateRentCars)
            context.state.dialogVisible=false
            context.dispatch('getToAuditCarsAsync')

            // context.dispatch('getForRentCarsAsync')
         },

    }

}